package com.kkwrite.regimen.provider.service.address;

import com.kkwrite.regimen.common.constant.DeletedEnum;
import com.kkwrite.regimen.common.entity.dto.address.RegPositionLogInDTO;
import com.kkwrite.regimen.common.entity.dto.address.RegPositionLogOutDTO;
import com.kkwrite.regimen.provider.domain.RegPositionLogDO;
import com.kkwrite.regimen.provider.repository.RegPositionRepository;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * Created by lisha on 2018/7/12 21:36.
 *
 * @author lisha
 */
@Service("positionService")
public class PositionServiceImpl implements PositionService {

    @Resource(name = "regPositionRepository")
    private RegPositionRepository positionRepository;

    @Override
    public RegPositionLogOutDTO saveOrUpdate(RegPositionLogInDTO inDTO) {
        RegPositionLogOutDTO out = new RegPositionLogOutDTO();
        RegPositionLogDO positionDO = new RegPositionLogDO();
        BeanUtils.copyProperties(inDTO, positionDO);
        positionDO.setDeleted(DeletedEnum.UN_DELETED.getValue());
        RegPositionLogDO queriedDo = positionRepository.findByUserId(positionDO.getUserId());
        if (queriedDo != null) {
            positionDO.setPositionLogId(queriedDo.getPositionLogId());
            positionDO.setDeleted(queriedDo.getDeleted());
        }
        positionRepository.saveAndFlush(positionDO);
        out.setSuccess("true");
        out.setMsg("执行成功");
        return out;
    }
}
